<template>
  <el-button type="warning" size="mini" :plain="!curDoc.good" @click="handleGood">
    {{ curDoc.good ? '取消精品' : '标记精品' }}
  </el-button>
</template>

<script>
import { mapState, mapActions } from 'vuex';
import { formatResource } from '@/components/spaces/resources-v2/format';

export default {
  name: 'mark-good-button',
  data() {
    return {
      loading: false,
    };
  },
  computed: {
    ...mapState({ curDoc: 'currentDocument' }),
  },
  methods: {
    ...mapActions({ setCurDoc: 'setCurrentDocument' }),
    handleGood() {
      this.loading = true;
      this.$api.spaceResources.handleGood({
        type: this.curDoc.type,
        spaceId: this.curDoc.space.id,
        docId: this.curDoc.id,
        state: !this.curDoc.good,
      }).then((res) => {
        this.$message.success('设置成功');
        this.setCurDoc(formatResource(res.data));
      }).catch(() => {})
        .then(() => { this.loading = false; });
    },
  },
};
</script>

<style></style>
